package com.hdd.hll.quartz.service;

import com.hdd.hll.modules.order.web.dto.PlanInfoDto;
import com.hdd.hll.modules.truck.web.dto.PassTruckDto;
import com.hdd.hll.quartz.entity.JobInfo;
import com.hdd.hll.quartz.entity.TriggerInfo;
import com.hdd.hll.quartz.job.PushOrderJob;
import com.hdd.hll.quartz.job.PushPassTruckJob;
import com.hdd.hll.quartz.jobbuilder.CustomJobBuilder;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * 车辆放行Job实现类
 *
 * @author jw
 */
@Service
@Slf4j
public class PushPassTruckJobService {

    private static final String JD_TRIGGER_GROUP_NAME = "JD_TRIGGER_GROUP";
    private static final String JD_PUSH_PASS_TRUCK_TRIGGER_PREFIX = "JD_PUSH_PASS_TRUCK_TRIGGER_";
    private static final String JD_JOB_GROUP_NAME = "JD_JOB_GROUP";
    private static final String JD_PUSH_PASS_TRUCK_JOB_PREFIX = "JD_PUSH_PASS_TRUCK_JOB_";

    @Autowired
    private CustomJobBuilder<PassTruckDto> customJobBuilder;

    public void registerPushPassTruckJob(PassTruckDto passTruckDto) {
        final String suffix = UUID.randomUUID().toString().replace("-", "");
        JobInfo jobInfo = new JobInfo();
        jobInfo.setName(JD_PUSH_PASS_TRUCK_JOB_PREFIX + suffix);
        jobInfo.setGroup(JD_JOB_GROUP_NAME);


        TriggerInfo triggerInfo = new TriggerInfo();
        triggerInfo.setName(JD_PUSH_PASS_TRUCK_TRIGGER_PREFIX + suffix);
        triggerInfo.setGroup(JD_TRIGGER_GROUP_NAME);
        try {
            customJobBuilder.register(jobInfo, triggerInfo, passTruckDto, PushPassTruckJob.class);
            log.info("注册JD车辆放行接口JOB成功");
        } catch (SchedulerException e) {
            log.error("注册JD车辆放行接口JOB失败，{}", e.getMessage());
        }
    }
}
